package com.xiangyi.teachingsystem.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.xiangyi.teachingsystem.entity.dto.MsgListDto;
import com.xiangyi.teachingsystem.entity.po.ShortMsg;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface ShortMsgMapper extends BaseMapper<ShortMsg> {
    @Select("(SELECT sm.send_id,sm.accept_id,sm.msg,sm.msg_time,sm.msg_space,u.`name` show_name,u.head_url show_url\n" +
            "FROM short_msg sm\n" +
            "LEFT JOIN `user` u\n" +
            "ON u.id=sm.accept_id\n" +
            "WHERE send_id=#{id}\n" +
            "AND msg_time IN\n" +
            "(SELECT MAX(msg_time) last_time\n" +
            "FROM short_msg\n" +
            "GROUP BY msg_space)\n" +
            "UNION\n" +
            "SELECT sm.send_id,sm.accept_id,sm.msg,sm.msg_time,sm.msg_space,u.`name` show_name,u.head_url show_url\n" +
            "FROM short_msg sm\n" +
            "LEFT JOIN `user` u\n" +
            "ON u.id=sm.send_id\n" +
            "WHERE accept_id=#{id}\n" +
            "AND msg_time IN\n" +
            "(SELECT MAX(msg_time) last_time\n" +
            "FROM short_msg\n" +
            "GROUP BY msg_space))\n" +
            "ORDER BY msg_time DESC")
    List<MsgListDto> msgList(Integer id);

    @Select("SELECT sm.*,u.`name` send_name,u.head_url send_url\n" +
            "FROM short_msg sm\n" +
            "LEFT JOIN `user` u\n" +
            "ON u.id=sm.send_id\n" +
            "WHERE msg_space=#{msgSpace}")
    List<ShortMsg> msgInfoList(String msgSpace);
}
